package LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-05-10
 * Time: 15:14
 */
public class MyLinkedList {

    static class ListNode{
        private ListNode prev;//前驱
        private ListNode next;//后继
        private int val;
        public ListNode(int val){
            this.val = val;
        }
    }
    private ListNode head;//头
    private ListNode last;//尾
    //头插法
    public void addFirst(int data){
       ListNode node = new ListNode(data);
       if(head==null){
           head =node;
           last = node;
       }else {
           node.next = head;
           head.prev = node;
           head = node;
       }
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head==null){
            head =node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }
    //寻找index的位置
    private ListNode searchIndex(int index){
        ListNode cur = head;
        while(index!=0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        //判断index合不合法
        if(index<0||index>size()){
            System.out.println("该下标不合法！！！");
        }
        ListNode node = new ListNode(data);
        //头插
        if(index==0){
            addFirst(data);
            return;
        }
        //尾插
        if(index==size()){
            addLast(data);
            return;
        }
        //中间插
        ListNode cur = searchIndex(index);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
        return;

    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur =cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
       while(cur!=null){
           if(cur.val==key){
               if(cur==head){
                   //头删
                   head = head.next;
                   if(head!=null&&head.next!=null) {
                       head.prev = null;
                   }
               }else {
                   //尾删和中间删除
                   cur.prev.next = cur.next;
                   if(cur.next!=null) {
                       cur.next.prev = cur.prev;
                   }else {
                       last = last.prev;
                   }
               }
           }else {
               cur = cur.next;
           }
           return;
       }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    //头删
                    head = head.next;
                    if(head!=null&&head.next!=null) {
                        head.prev = null;
                    }
                }else {
                    //尾删和中间删除
                    cur.prev.next = cur.next;
                    if(cur.next!=null) {
                        cur.next.prev = cur.prev;
                    }else {
                        last = last.prev;
                    }
                }
            }else {
                cur = cur.next;
            }
            cur =cur.next;
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int count=0;
        while(cur!=null){
           ++count;
            cur =cur.next;
        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur =cur.next;
        }
        System.out.println();
    }
    public void clear(){
        ListNode cur = head;
        while(cur!=null){
            ListNode curNext = cur.next;
            cur.prev=null;
            cur.next=null;
            cur =curNext;
        }
        head = null;
        last = null;
    }


}
